﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using KPIS.GERP.Models;
using KPIS.GERP.Models.Class;
using KPIS.GERP.Models.ICM;
using KPIS.GERP.Models.SYS;
using System.Data;
using System.Data.Entity;
using System.Linq.Dynamic;
using System.Web.Mvc;


namespace KPIS.GERP.WCF.GIMS
{
    [ServiceContract]
    public interface ICommitteeGroupService
    {
        [OperationContract]
        IEnumerable<CommitteeGroup> Select(CommitteeGroup filter = null, List<string> includeSubObjectString = null);

        [OperationContract]
        int Insert(CommitteeGroup model, int createByUserId);

        [OperationContract]
        void Update(CommitteeGroup model, int updateByUserId);

        [OperationContract]
        void Delete(int committeeGroupId, int deleteByUserId); 
    }

    // NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "CommitteeService" in code, svc and config file together.
    // NOTE: In order to launch WCF Test Client for testing this service, please select CommitteeService.svc or CommitteeService.svc.cs at the Solution Explorer and start debugging.
    [ServiceErrorBehavior(typeof (ElmahErrorHandler))]
    public class CommitteeGroupService : ICommitteeGroupService
    {
        // GERPContext db = new GERPContext();      
        public IEnumerable<CommitteeGroup> Select(CommitteeGroup criteriaModel = null, List<string> includeSubObjectString = null)
        {
            try
            {
                using (var context = new GERPContext(false))
                {
                    IQueryable<CommitteeGroup> query = context.CommitteeGroups;

                    if (includeSubObjectString != null)
                    {
                        query = includeSubObjectString.Aggregate(query, (current, includePath) => current.Include(includePath));
                    }

                    if (criteriaModel != null)
                    {
                        object[] param;
                        query = query.Where(KPIS.GERP.Models.Class.Utility.GenerateWhereString(criteriaModel, out param), param);
                    }

                    var x = query.ToList();
                    foreach(var committeeGroup in x)
                    {
                        if(committeeGroup.Committees == null) continue;

                        foreach(var commitee in committeeGroup.Committees)
                        {
                            if(commitee.CommitteeGroup != null && commitee.CommitteeGroup.Committees != null)
                            {
                                foreach(var c2 in commitee.CommitteeGroup.Committees)
                                {
                                    c2.CommitteeGroup = null;
                                }
                            }
                        }
                    }
                    return x;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public int Insert(CommitteeGroup model, int createByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    model.RecordStatus = RecordStatus.UsedStatus;
                    context.CommitteeGroups.Add(model);
                    context.SaveChanges();

                    return model.CommitteeGroupId;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Update(CommitteeGroup model, int updateByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    context.Entry(model).State = EntityState.Modified;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public void Delete(int id, int deleteByUserId)
        {
            try
            {
                using (var context = new GERPContext())
                {
                    var modelToDelete = context.CommitteeGroups.Find(id);
                    if (modelToDelete == null) return;

                    context.Entry(modelToDelete).State = EntityState.Modified;
                    modelToDelete.RecordStatus = RecordStatus.DeletedStatus;
                    context.SaveChanges();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}